﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;

namespace MoneyBook.Controls {

    public class GroupingViewItem : ContentControl {

        private GroupingView owner = null;
        private object source = null;

        public bool IsSelected {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(GroupingViewItem), new PropertyMetadata(false, OnIsSelectedChanged));

        private static void OnIsSelectedChanged(object sender, DependencyPropertyChangedEventArgs e) {
            GroupingViewItem item = sender as GroupingViewItem;
            if (item != null) {
                item.OnIsSelectedChanged((bool)e.NewValue, (bool)e.OldValue);
            }
        }

        private void OnIsSelectedChanged(bool newValue, bool oldValue) {
            if (newValue == oldValue) {
                return;
            }
            if (newValue) {
                owner.UpdateSelectedItem(this);
            }
            UpdateState();
        }

        protected override void OnKeyDown(KeyEventArgs e) {
            this.owner.OnItemKeyDown(this, e);
        }


        public GroupingViewItem(GroupingView owner) {
            this.DefaultStyleKey = typeof(GroupingViewItem);
            this.owner = owner;
        }

        internal object Source { 
            get{return source;}
            set {
                this.source = value;
                this.DataContext = this.source;
                this.Content = this.source;
            }
        }

        public override void OnApplyTemplate() {
            base.OnApplyTemplate();

            UpdateState();
        }

        protected override void OnGotFocus(RoutedEventArgs e) {
            VisualStateManager.GoToState(this, "Focused", false);
        }

        protected override void OnLostFocus(RoutedEventArgs e) {
            VisualStateManager.GoToState(this, "Unfocused", false);
        }

        protected override void OnMouseEnter(MouseEventArgs e) {
            VisualStateManager.GoToState(this, "MouseOver", false);
        }

        protected override void OnMouseLeave(MouseEventArgs e) {
            VisualStateManager.GoToState(this, "Normal", false);
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
            if (!this.IsSelected) {
                this.IsSelected = true;
            } else {
                if (this != FocusManager.GetFocusedElement()) {
                    this.Focus();
                }
            }
            
            //owner.PrepareItemSelection(this);
            e.Handled = true;
        }

        private void UpdateState() {
            if (this.IsSelected) {
                VisualStateManager.GoToState(this, "Selected", false);
            } else {
                VisualStateManager.GoToState(this, "Unselected", false);
            }
        }
    }
}
